package DBMailDAO;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import mail.User;
import mailDAO.UserDAO;

public class DBUserDAO implements UserDAO{
	private SessionFactory sessionFactory;

	public DBUserDAO() {
		Configuration config=new Configuration().configure();  
		sessionFactory=config.buildSessionFactory();
		
	}

	public User getUesrById(long id){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			User user;
			user = (User) sn.get(User.class, id);			
			return user;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public User getUserByUsername(String username) {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{
			User user=null;
			Query query = sn.createQuery("from User u where u.username = :username");
			query.setString("username", username);
			user = (User) query.uniqueResult();
			return user;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}
	
	public List<User> listAllUsers() {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{			
			Query query = sn.createQuery("from User");
			List<User> userList = query.list();
			return userList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}

	public List<User> listLockedUsers() {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{			
			Query query = sn.createQuery("from User u where u.locked = :locked");
			//query.setString("nick", "chun");
			query.setBoolean("locked", true);
			List<User> userList = query.list();
			return userList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}

	public List<User> listUnlockedUsers() {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{		
			
			Query query = sn.createQuery("from User u where u.locked != :locked");			
			query.setBoolean("locked", true);
			List<User> userList = query.list();
			return userList;
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}
		return null;
	}

	public void saveUser(User user) {		
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{				
			sn.save(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}				
	}

	public void deleteUser(User user){
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{				
			sn.delete(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}		
	}
	
	public void setUserLocked(User user) {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{				
			user.setLocked(true);
			sn.saveOrUpdate(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}	
		
	}

	public void setUserUnlocked(User user) {
		Session sn = sessionFactory.openSession();
		Transaction tn = sn.beginTransaction();
		try{	
			user.setLocked(false);
			sn.saveOrUpdate(user);
		}catch(Exception e){
			e.printStackTrace();
		}
		finally{
			tn.commit();
			sn.close();
		}	
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
